<?php
class GDImage {
	var $sourcePath;
	var $galleryPath;
	var $toFile = true;
	var $fontName;
	var $maxWidth = 500;
	var $maxHeight = 600;
	function GDImage($sourcePath, $galleryPath, $fontPath = null) {
		$this->sourcePath = $sourcePath;
		$this->galleryPath = $galleryPath;
	
	}
	function makeThumb($sourFile, $width = 120, $height = 120) {
		$imageInfo = $this->getInfo ( $sourFile );
		$sourFile = $this->sourcePath . $sourFile;
		$tmpParts = explode ( ".", $imageInfo ["name"] );
		
		$file_ext = $tmpParts [1];
		$newName = substr ( $imageInfo ["name"], 0, strrpos ( $imageInfo ["name"], "." ) ) . "." . $file_ext;
		switch ($imageInfo ["type"]) {
			case 1 :
				$img = imagecreatefromgif ( $sourFile );
				break;
			case 2 :
				$img = imagecreatefromjpeg ( $sourFile );
				break;
			case 3 :
				$img = imagecreatefrompng ( $sourFile );
				break;
			default :
				return 0;
				break;
		}
		if (! $img)
			return 0;
		
		$width = ($width > $imageInfo ["width"]) ? $imageInfo ["width"] : $width;
		$height = ($height > $imageInfo ["height"]) ? $imageInfo ["height"] : $height;
		$srcW = $imageInfo ["width"];
		$srcH = $imageInfo ["height"];
		if ($srcW * $width > $srcH * $height)
			$height = round ( $srcH * $width / $srcW );
		else
			$width = round ( $srcW * $height / $srcH );
		
		if (function_exists ( "imagecreatetruecolor" )) {
			$new = imagecreatetruecolor ( $width, $height );
			ImageCopyResampled ( $new, $img, 0, 0, 0, 0, $width, $height, $imageInfo ["width"], $imageInfo ["height"] );
		} else {
			$new = imagecreate ( $width, $height );
			ImageCopyResized ( $new, $img, 0, 0, 0, 0, $width, $height, $imageInfo ["width"], $imageInfo ["height"] );
		}
		
		if ($this->toFile) {
			if (file_exists ( $this->galleryPath . $newName ))
				unlink ( $this->galleryPath . $newName );
			ImageJPEG ( $new, $this->galleryPath . $newName );
			return $this->galleryPath . $newName;
		} else {
			ImageJPEG ( $new );
		}
		ImageDestroy ( $new );
		ImageDestroy ( $img );
	
	}
	
	function waterMark($sourFile, $text) {
		$imageInfo = $this->getInfo ( $sourFile );
		$sourFile = $this->sourcePath . $sourFile;
		$newName = substr ( $imageInfo ["name"], 0, strrpos ( $imageInfo ["name"], "." ) ) . "_mark.jpg";
		switch ($imageInfo ["type"]) {
			case 1 :
				$img = imagecreatefromgif ( $sourFile );
				break;
			case 2 :
				$img = imagecreatefromjpeg ( $sourFile );
				break;
			case 3 :
				$img = imagecreatefrompng ( $sourFile );
				break;
			default :
				return 0;
				break;
		}
		if (! $img)
			return 0;
		
		$width = ($this->maxWidth > $imageInfo ["width"]) ? $imageInfo ["width"] : $this->maxWidth;
		$height = ($this->maxHeight > $imageInfo ["height"]) ? $imageInfo ["height"] : $this->maxHeight;
		$srcW = $imageInfo ["width"];
		$srcH = $imageInfo ["height"];
		if ($srcW * $width > $srcH * $height)
			$height = round ( $srcH * $width / $srcW );
		else
			$width = round ( $srcW * $height / $srcH );
		
		if (function_exists ( "imagecreatetruecolor" )) {
			$new = imagecreatetruecolor ( $width, $height );
			ImageCopyResampled ( $new, $img, 0, 0, 0, 0, $width, $height, $imageInfo ["width"], $imageInfo ["height"] );
		} else {
			$new = imagecreate ( $width, $height );
			ImageCopyResized ( $new, $img, 0, 0, 0, 0, $width, $height, $imageInfo ["width"], $imageInfo ["height"] );
		}
		$white = imageColorAllocate ( $new, 255, 255, 255 );
		$black = imageColorAllocate ( $new, 0, 0, 0 );
		$alpha = imageColorAllocateAlpha ( $new, 230, 230, 230, 40 );
		
		ImageFilledRectangle ( $new, 0, $height - 26, $width, $height, $alpha );
		ImageFilledRectangle ( $new, 13, $height - 20, 15, $height - 7, $black );
		ImageTTFText ( $new, 4.9, 0, 20, $height - 14, $black, $this->fontName, $text [0] );
		ImageTTFText ( $new, 4.9, 0, 20, $height - 6, $black, $this->fontName, $text [1] );
		
		if ($this->toFile) {
			if (file_exists ( $this->galleryPath . $newName ))
				unlink ( $this->galleryPath . $newName );
			ImageJPEG ( $new, $this->galleryPath . $newName );
			return $this->galleryPath . $newName;
		} else {
			ImageJPEG ( $new );
		}
		ImageDestroy ( $new );
		ImageDestroy ( $img );
	
	}
	
	function displayThumb($file) {
		$thumbName = substr ( $file, 0, strrpos ( $file, "." ) ) . "_thumb.jpg";
		$file = $this->galleryPath . $thumbName;
		if (! file_exists ( $file ))
			return 0;
		$html = "<img src='$file' style='border:1px solid #000'/>";
		echo $html;
	}
	
	function displayMark($file) {
		$markName = substr ( $file, 0, strrpos ( $file, "." ) ) . "_mark.jpg";
		$file = $this->galleryPath . $markName;
		if (! file_exists ( $file ))
			return 0;
		$html = "<img src='$file' style='border:1px solid #000'/>";
		echo $html;
	}
	
	function getInfo($file) {
		$file = $this->sourcePath . $file;
		
		$data = getimagesize ( $file );
		$imageInfo ["width"] = $data [0];
		$imageInfo ["height"] = $data [1];
		$imageInfo ["type"] = $data [2];
		$imageInfo ["name"] = basename ( $file );
		return $imageInfo;
	}
}
?>